<?php

/**
 * commentaire actions.
 *
 * @package    explications_informatiques
 * @subpackage gifts
 * @author     Guimezanes Cyrille
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class pointActions extends sfActions
{
  public function executeResume(sfWebRequest $request)
  {
	$this->forwardUnless($this->getUser()->isAuthenticated() && $this->getUser()->hasCredential("HAS_POINT"),"headers","forbidden");
	$q = Doctrine_Query::create()
	 	->from('Point c')
		->where("c.id_client = ?",$this->getUser()->getGuardUser()->getId())
		->orderBy("c.id DESC")
		->limit (15);
	$this->points = $q->execute();
  }
  public function executeShop(sfWebRequest $request)
  {
	$this->forwardUnless($this->getUser()->isAuthenticated() && $this->getUser()->hasCredential("HAS_POINT"),"headers","forbidden");
	$q = Doctrine_Query::create()
	 	->from('Shop c')
		->where("c.online = ?","1");
	$this->goods = $q->execute();
  }
  function buy_skin($arg)
  {
	  $skin = new SkinsOwned();
	  $skin->setIdClient($this->getUser()->getGuardUser()->getId());
	  $skin->setIdSkin(intval($arg));
	  $skin->save();
	  $this->getUser()->setFlash("info","Votre skin à été ajouté à votre liste dans votre profil");	
	  return true;
  }
  public function executeBuy(sfWebRequest $request)
  {
	 $this->forwardUnless($this->getUser()->isAuthenticated() && $this->getUser()->hasCredential("HAS_POINT"),"headers","forbidden"); 
	 $q = Doctrine_Query::create()
	 	->from('Shop c')
		->where("c.online = ?","1")
		->andWhere("c.id = ?",$request["good"]);
	$good = $q->fetchOne();
	$this->forward404Unless($good);
	
	if ($this->getUser()->getProfile()->getNbPoint() >= $good["prix"])
	{
		if(call_user_func(array("pointActions", $good["function_to_call"]), $good["argument"]))
		{
			$sell = new Sells();
			$sell->setIdClient($this->getUser()->getGuardUser()->getId());
			$sell->setIdShop($good["id"]);
			$sell->setDate(date("Ymdhis"));
			$sell->save();
			$this->getUser()->getProfile()->decrPoint($good["prix"],"Achat dans le magasin");
			//
		}
		else
			$this->getUser()->setFlash("err","Une erreur est survenue durant l'achat");
			
		if (!$this->getUser()->hasFlash("info"))
			$this->getUser()->setFlash("info","Achat effectué. Merci!");
	}
	else
		$this->getUser()->setFlash("err","Crédit insuffisant!");
	$this->redirect("point/shop");
  }
  
  public function executeBonus(sfWebRequest $request)
  {
	 $this->forwardUnless($this->getUser()->isAuthenticated() && $this->getUser()->hasCredential("GIVE_BONUS"),"headers","forbidden");
  }
}
